﻿using EpubFactory.Common;
using EpubFactory.Template.Mobi;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EpubFactory.Template
{
    public class MobiCreator : TemplateCreator
    {
        public override void Generate(Book book, string targetFile)
        {
            var target = targetFile + ".epub";
            DeleteFile(target);
            var tmpDir = CreateTempDir(book);
            LoadTemplate();
            CreateCover(book, tmpDir);
            CreateChapters(book, tmpDir);
            CreateStyle(book, tmpDir);
            CreateBookToc(book, tmpDir);
            CreateNaxToc(book, tmpDir);
            CreateOpf(book, tmpDir);
            Gen(book, tmpDir, target);
        }
        public string styleContent;
        private void LoadTemplate()
        {
            if (!string.IsNullOrWhiteSpace(styleContent))
            {
                return;
            }
            using (ZipArchive archive = ZipFile.OpenRead(TemplateFile))
            {
                foreach (ZipArchiveEntry entry in archive.Entries)
                {
                    string text = null;
                    using (var reader = new StreamReader(entry.Open()))
                    {
                        text = reader.ReadToEnd();
                    }
                    switch (entry.Name)
                    {
                        case "style.css":
                            styleContent = text;
                            break;
                    }
                }
            }
        }


        private void Gen(Book book, string tmpDir, string targetFile)
        {
            string binPath = Path.Combine(Directory.GetCurrentDirectory(), "Tools", "kindlegen.exe");
            string param = "content.opf -c1 -o book.mobi";
            ProcessStartInfo p = null;
            Process proc;
            p = new ProcessStartInfo(binPath, param);
            p.UseShellExecute = false;
            p.RedirectStandardOutput = true;
            p.WorkingDirectory = tmpDir;
            proc = Process.Start(p);
            string output = proc.StandardOutput.ReadToEnd();
            //_log.Debug(output);
            proc.WaitForExit();
            var target = Path.Combine(tmpDir, "book.mobi");
            if (File.Exists(target))
            {
                File.Copy(target, targetFile);
            }

        }

        private void CreateNaxToc(Book book, string tmpDir)
        {
            var template = new TocNcxTemplate(book);
            File.WriteAllText(Path.Combine(tmpDir, "toc.ncx"), template.TransformText());
        }

        private void CreateOpf(Book book, string tmpDir)
        {
            var template = new ContentOPFTemplate(book);
            File.WriteAllText(Path.Combine(tmpDir, "content.opf"), template.TransformText());
        }

        private void CreateBookToc(Book book, string tmpDir)
        {
            var template = new TocHtmlTemplate(book);
            File.WriteAllText(Path.Combine(tmpDir, "toc.html"), template.TransformText());
        }

        private void CreateStyle(Book book, string tmpDir)
        {
            File.WriteAllText(Path.Combine(tmpDir, "style.css"), styleContent);
        }

        private void CreateChapters(Book book, string tmpDir)
        {
            foreach (var chapter in book.Chapters)
            {
                var template = new ChapterHtmlTemplate(chapter);
                File.WriteAllText(Path.Combine(tmpDir, string.Format("chapter-{0}.html", chapter.Id)), template.TransformText());
            }
        }

        private void CreateCover(Book book, string tmpDir)
        {
            var template = new CoverHtmlTemplate(book);
            File.WriteAllText(Path.Combine(tmpDir, "cover.html"), template.TransformText());
        }
    }
}
